package com.treasure.hunt.popup

import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import com.treasure.hunt.R
import kotlinx.android.synthetic.main.popup_dialog_hint.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

class DialogHintPopup(
    context: Context,
    val onConfirm: (view: View, type: Type) -> Unit,
    val onCancel: (view: View, type: Type) -> Unit
) : BasePopupWindow(context) {


    private var type: Type = Type.SHOW_NULL

    init {
        setBlurBackgroundEnable(true)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_hint)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogHintConfirm.setOnClickListener {
            this.onConfirm(it, type)
        }
        contentView.tvDialogHintCancel.setOnClickListener {
            this.onCancel(it, type)
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun setTextTitle(text: String): DialogHintPopup {
        contentView.tvDialogHintTitle.text = text
        return this
    }

    fun setTextMessage(text: String): DialogHintPopup {
        contentView.tvDialogHintMessage.text = text
        return this
    }

    fun setTextConfirm(text: String): DialogHintPopup {
        contentView.tvDialogHintConfirm.text = text
        return this
    }

    fun setTextCancel(text: String): DialogHintPopup {
        contentView.tvDialogHintCancel.text = text
        return this
    }

    fun setType(type: Type): DialogHintPopup {
        this.type = type
        return this
    }

    fun setGoneTextCancel(isGone: Boolean = true) : DialogHintPopup {
        contentView.tvDialogHintCancel.visibility = if (isGone) View.GONE else View.VISIBLE
        contentView.divider.visibility = if (isGone) View.GONE else View.VISIBLE
        return this
    }

    fun setText(
        textTitle: String, textMessage: String,
        textConfirm: String, textCancel: String
    ): DialogHintPopup {
        contentView.tvDialogHintTitle.text = textTitle
        contentView.tvDialogHintMessage.text = textMessage
        contentView.tvDialogHintConfirm.text = textConfirm
        contentView.tvDialogHintCancel.text = textCancel
        return this
    }

    enum class Type(var type: Int) {
        SHOW_NULL(0),
        SHOW_PHONE(1),
        SHOW_SYNTHESIS(2),
        SHOW_HUNT(3),
        SHOW_ERROR(4),
        SHOW_UPDATE(5)
    }
}